package com.codechuan.demo.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.floatWindowManager.log.LogManager;
import com.codechuan.demo.utils.HttpControllerUtil;
import com.codechuan.demo.utils.MyHttpUtil;

/**
 * @Deprecated 检查是否接单成功线程
 */
public class CheckJDThread implements Runnable {
    @Override
    public void run() {
        // 判断是否接单成功
        while (Constant.isStart) {
            JSONObject jsonObject = HttpControllerUtil.getSuccessOrder();
            if (jsonObject != null && Constant.CHECK_OK) {
                if (jsonObject.getString("code").equals("0")) {
                    // 获取行程数组
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (Object datum : data) {
                        JSONObject dataList = JSONObject.parseObject(datum.toString());
                        // 获取订单列表
                        JSONArray paxOrderList = dataList.getJSONArray("paxOrderList");
                        for (Object o : paxOrderList) {
                            JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
                            // 获取订单ID
                            String string = jsonObject1.getString("passengerJourneyGuid");
                            if (Constant.orderList.toString().isEmpty()) {
                                LogManager.get().addLog("接单成功:" + jsonObject1);
                                if (!Constant.mediaPlayerPK.isPlaying()) {
                                    Constant.mediaPlayerPK.start();
                                }
                                // 判断是否连续中单
                                if (!Constant.LX_QD)
                                {
                                    LogManager.get().stopLog();
                                    Constant.isStart = false;
                                    Constant.handler.post(()->{
                                        Constant.button.setText("开始抢单");

                                    });
                                }
                            } else {
                                if (!Constant.orderList.toString().contains(string)) {
                                    LogManager.get().addLog("接单成功:" + jsonObject1);
                                    if (!Constant.mediaPlayerPK.isPlaying()) {
                                        Constant.mediaPlayerPK.start();
                                    }

                                    // 判断是否连续中单
                                    if (!Constant.LX_QD)
                                    {
                                        LogManager.get().stopLog();
                                        Constant.isStart = false;
                                        Constant.handler.post(()->{
                                            Constant.button.setText("开始抢单");

                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}